package com.cashmate.struts;

import com.cashmate.hibernate.dao.LoansDAO;
import com.cashmate.hibernate.dao.LoanverificationDAO;
import com.cashmate.hibernate.pojo.Loans;
import com.cashmate.hibernate.pojo.Loanverification;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Result;


import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class Request_EditLoanAppForRequest extends ActionSupport {

    private LoanverificationDAO loanverDAO = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private LoansDAO loanDAO = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private String releasedate;
    private int amountrequested;
    private String firstlast;
    private String secondlast;
    private String oper;
    private int id;
    private String loc;

    @Actions({
        @Action(value = "/editloanapp", results = {
            @Result(location = "${loc}", name = "success"),
            @Result(location = "${loc}", name = "input")
        })
    })
    @Override
    public String execute() throws Exception {
        loc = "../../loanrequest.jsp?cat=request";

        if (releasedate != null) {
            Loanverification la = loanverDAO.get(id);
            la.setReleasedate(releasedate);
            loanverDAO.update(la);
        }

        if (firstlast != null) {
            Loanverification la = loanverDAO.get(id);
            if (la.getFirstlastpn() > 0) {
                Loans loan = la.getBorrowers().getFirstlast();
                loan.setModeofpayment(firstlast);
                loanDAO.update(loan);
            }

        }

        if (secondlast != null) {
            Loanverification la = loanverDAO.get(id);
            if (la.getSecondlastpn() > 0) {
                Loans loan = la.getBorrowers().getSecondlast();
                loan.setModeofpayment(secondlast);
                loanDAO.update(loan);
            }
        }

        if (amountrequested > 0) {
            Loanverification la = loanverDAO.get(id);
            la.setAmountrequested(amountrequested);
            loanverDAO.update(la);
        }
        return SUCCESS;
    }

    public void setOper(String oper) {
        this.oper = oper;
    }

    public void setFirstlast(String firstlast) {
        this.firstlast = firstlast;
    }

    public void setReleasedate(String releasedate) {
        this.releasedate = releasedate;
    }

    public void setSecondlast(String secondlast) {
        this.secondlast = secondlast;
    }

    public void setAmountrequested(int amountrequested) {
        this.amountrequested = amountrequested;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLoc() {
        return loc;
    }
}
